package com.goods.controller.business;


import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/productCategory")
@CrossOrigin
public class ProductCategoryController {

    @Autowired
    ProductCategoryService businessService;


    //页面初始化加载物资类别
    @GetMapping("/categoryTree")
    public ResponseBean<Map<Object, Object>> list(Integer pageSize,Integer pageNum){
        List<ProductCategory> categoryList = businessService.getList();
        List<ProductCategoryTreeNodeVO> collect = categoryList.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO p = new ProductCategoryTreeNodeVO();
            p.setId(productCategory.getId());
            p.setName(productCategory.getName());
            p.setRemark(productCategory.getRemark());
            p.setSort(productCategory.getSort());
            p.setCreateTime(productCategory.getCreateTime());
            p.setModifiedTime(productCategory.getModifiedTime());
            p.setPid(productCategory.getPid());
            return p;
        }).collect(Collectors.toList());
        Map<Object, Object> map = new HashMap<>();
        List<ProductCategoryTreeNodeVO> l = CategoryTreeBuilder.build(collect);

        if (pageSize != null && pageNum != null){
            List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(l, pageSize, pageNum);
            map.put("rows",page);
        }else {
            map.put("rows",l);
        }

        map.put("total",l.size());

        return ResponseBean.success(map);

        //http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
    }

    //添加时加载物资类别
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> categoryTree(){
        List<ProductCategory> list = businessService.categoryTree();

        List<ProductCategoryTreeNodeVO> collect = list.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO p = new ProductCategoryTreeNodeVO();
            p.setId(productCategory.getId());
            p.setName(productCategory.getName());
            p.setRemark(productCategory.getRemark());
            p.setSort(productCategory.getSort());
            p.setCreateTime(productCategory.getCreateTime());
            p.setModifiedTime(productCategory.getModifiedTime());
            p.setPid(productCategory.getPid());

            return p;
        }).collect(Collectors.toList());

        List<ProductCategoryTreeNodeVO> nodeVOS = CategoryTreeBuilder.buildParent(collect);

        return ResponseBean.success(nodeVOS);
    }

    //添加物资类别
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        int i = businessService.add(productCategoryVO);
        if (i == 0){
            return ResponseBean.error("添加失败");
        }else {
            return ResponseBean.success();
        }

    }

    //编辑物资类别(数据回显)
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategory productCategory = businessService.edit(id);
        if (productCategory == null){
            return ResponseBean.error("分类信息不存在");
        }else {
            return ResponseBean.success(productCategory);
        }
    }

    //编辑物资类别(数据保存)
    @PutMapping("/update/{id}")
    public ResponseBean update(
            @PathVariable Long id,
            @RequestBody ProductCategoryVO productCategoryVO
    ){
        int i = businessService.update(id,productCategoryVO);
        if (i == 0){
            return ResponseBean.error("修改失败");
        }else {
            return ResponseBean.success();
        }
    }

    //删除物资类别
    @DeleteMapping("/productCategory/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = businessService.delete(id);
        if (i == 0){
            return ResponseBean.error("删除失败");
        }else {
            return ResponseBean.success();
        }
    }
}
